SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[SP_TCMOVI_CAJA_Q11]
/*---------------------------------------------------------*/
/*---Empresa              : OFISIS S.A.                  --*/
/*---Cliente              : OFISIS S.A.                  --*/
/*---Sistema              : Tesoreria                    --*/
/*---M"dulo               : Empresa                      --*/
/*---Programa             : Reporte Cuadre Caja Diario   --*/
/*---Script               : ttproc_i.sql                 --*/
/*---Nombre SP            : SP_TCMOVI_CAJA_Q11           --*/
/*---Desarrollado por     : Javier Flores A.             --*/
/*---Fecha Creaci"n       : 25/02/2003                   --*/
/*---Base Datos           : Microsoft Sql Server         --*/
/*---Versi"n              : 8.0                          --*/
/*---Invoca a SP          :                              --*/
/*---------------------------------------------------------*/

/*---------------------------------------------------------*/
/*---Modificado 1 por     :                              --*/
/*---Fecha Modificaci"n   :                              --*/ 
/*---Detalle Modificaci"n :                              --*/
/*---------------------------------------------------------*/

/*---------------------------------------------------------*/
/*---Comentarios                                         --*/
/*---                                                    --*/
/*---                                                    --*/
/*---------------------------------------------------------*/

/*---------------------------------------------------------*/
/*---Drop Proc SP_TCMOVI_CAJA_Q11                        --*/
/*---Grant all on SP_TCMOVI_CAJA_Q11 to public           --*/ 
/*---SP_TCMOVI_CAJA_Q11 				 --*/
/*--- 						         --*/
/*---------------------------------------------------------*/
@ISCO_EMPR TD_VC_003,
@ISCO_USUA TD_VC_008,
@IDFE_OPER TD_DT_001,
@ISCO_CAJA TD_VC_008
	As
Declare 

  @VNNU_FILA   TD_IN_001,
  @VSCO_MONE   TD_VC_003

CREATE TABLE #TWCUAD_CAJA_DIAR_1
( CO_EMPR      VARCHAR(2) NULL,
  CO_CAJA      VARCHAR(8) NULL,
  SA_INIC_EFEC NUMERIC(16,4) NULL,
  SA_INIC_REPR NUMERIC(16,4) NULL,
  SA_INIC_TOCA NUMERIC(16,4) NULL,
  IN_BNCO      NUMERIC(16,4) NULL,
  TO_INGR_BNCO NUMERIC(16,4) NULL,
  EG_GAST_DIAS NUMERIC(16,4) NULL,
  NU_SALD_CAJA NUMERIC(16,4) NULL,
  CI_CAJA_PROV NUMERIC(16,4) NULL,
  CI_CAJA_EFEC NUMERIC(16,4) NULL,
  CI_CAJA_TOTA NUMERIC(16,4) NULL
)

CREATE TABLE #TWCUAD_CAJA_DIAR_2
( CO_EMPR      VARCHAR(2)    NULL,
  CO_CAJA      VARCHAR(8)    NULL,
  IM_INGR      NUMERIC(16,4) NULL,
  IM_EGRE      NUMERIC(16,4) NULL,
  SA_INIC_REPR NUMERIC(16,4) NULL,
  SA_INIC_EFEC NUMERIC(16,4) NULL,
  IN_BNCO      NUMERIC(16,4) NULL,
  EG_GAST_DIAS NUMERIC(16,4) NULL,
  NU_SOLI      VARCHAR(10)   NULL,
  IM_GAST      NUMERIC(16,4) NULL,
  IM_PROV      NUMERIC(16,4) NULL)

CREATE TABLE #TWCUAD_CAJA_DIAR_3
( CO_EMPR      VARCHAR(2)    NULL,
  CO_CAJA      VARCHAR(8)    NULL,
  IM_PROV      NUMERIC(16,4) NULL,
  NU_SOLI      VARCHAR(10)   NULL)

/* EFECTIVO */

/* SE INSERTA EL SALDO INICIAL */

SELECT @VSCO_MONE = CO_MONE
FROM TMCAJA
WHERE CO_EMPR = @ISCO_EMPR
AND   CO_CAJA = @ISCO_CAJA

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND   T1.TI_MOVI = 'I'
AND   T1.FE_OPER < @IDFE_OPER
AND   T1.CO_TIPO_OPER = 'SIN'
AND   T1.TI_SITU != 'ANU'
AND   EXISTS   (Select  T2.CO_EMPR	
	       From   TTUSER_CAJA T2
	       Where  T1.CO_EMPR = T2.CO_EMPR
	       And    T1.CO_CAJA = T2.CO_CAJA
	       And    T2.CO_USUA = @ISCO_USUA
	       And    T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

--INGRESOS
INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND   T1.TI_MOVI = 'I'
AND   T1.FE_OPER < @IDFE_OPER
AND   T1.CO_TIPO_OPER != 'SIN'
AND   T1.TI_SITU != 'ANU'
AND   EXISTS   (Select  T2.CO_EMPR	
	       From   TTUSER_CAJA T2
	       Where  T1.CO_EMPR = T2.CO_EMPR
	       And    T1.CO_CAJA = T2.CO_CAJA
	       And    T2.CO_USUA = @ISCO_USUA
	       And    T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

-- EGRESOS

/* GASTOS DIRECTOS */

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER < @IDFE_OPER
AND    (T1.CO_TIPO_OPER != 'A20' AND T1.CO_TIPO_OPER != 'SIP' and T1.CO_TIPO_OPER != 'A73'  )
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

/* Recibos provisionales */

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER < @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    (T1.CO_TIPO_OPER != 'SIP' AND T1.CO_TIPO_OPER = 'A20' )
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA,SA_INIC_EFEC )
SELECT CO_EMPR,CO_CAJA, ISNULL(SUM( ISNULL(IM_INGR,0) - ISNULL(IM_EGRE,0)),0)
FROM #TWCUAD_CAJA_DIAR_2
GROUP BY CO_EMPR, CO_CAJA

DELETE #TWCUAD_CAJA_DIAR_2

/*RECIBOS PROVISIONALES */

-- Provisionales

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR, NU_SOLI)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(PATINDEX('E', T1.TI_MOVI)*ISNULL(T1.IM_MOVI,0)),0)
-ISNULL(SUM(PATINDEX('I', T1.TI_MOVI)*ISNULL(T1.IM_MOVI,0)),0)
       , T1.NU_SOLI
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
--AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER < @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
--AND    T1.CO_TIPO_OPER = 'A20'
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA, T1.NU_SOLI

-- Gastos Provisionales 

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)--, NU_SOLI)
SELECT T1.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)--, NU_SOLI
FROM   TCMOVI_CAJA T1, TMCAJA T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
--AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA != '00000013' and T1.CO_CAJA != '00000014' and T1.CO_CAJA != '00000015'
       and T1.CO_CAJA != '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER < @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    (T1.CO_TIPO_OPER != 'A20' AND T1.CO_TIPO_OPER != 'D12')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
AND    T1.CO_EMPR = T2.CO_EMPR
AND    T1.CO_CAJA = T2.CO_CAJA
AND    T2.CO_MONE = @VSCO_MONE
GROUP BY T1.CO_EMPR, T1.CO_CAJA--, T1.NU_SOLI

-- 0

/*INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   TCMOVI_CAJA T1, #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.CO_TIPO_OPER != 'A55'
AND    T1.FE_OPER < @IDFE_OPER
AND    T1.TI_SITU != 'ANU'
AND    T1.NU_SOLI = T2.NU_SOLI
GROUP BY T1.CO_EMPR, T1.CO_CAJA*/

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, SA_INIC_REPR )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM( ISNULL(IM_INGR,0) - ISNULL(IM_EGRE,0)),0)
FROM #TWCUAD_CAJA_DIAR_2
WHERE  ( ( @ISCO_CAJA IS NOT NULL AND CO_CAJA = @ISCO_CAJA ) OR CO_CAJA IS NULL )	
GROUP BY CO_EMPR, CO_CAJA

/* CALCULANDO TOTAL CAJA */

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR,CO_CAJA, SA_INIC_TOCA )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM( ISNULL(SA_INIC_EFEC,0) + ISNULL(SA_INIC_REPR,0)),0)
FROM #TWCUAD_CAJA_DIAR_1
GROUP BY CO_EMPR, CO_CAJA

/* INGRESOS A BANCOS */

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, IN_BNCO )
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM TCMOVI_CAJA T1, TDMOVI_RECA T2
WHERE ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND   ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND   T1.TI_MOVI = 'I'
AND   T1.FE_OPER = @IDFE_OPER
AND   T1.TI_SITU != 'ANU'
AND   T1.CO_EMPR = T2.CO_EMPR_DEST
AND   T1.CO_CAJA = T2.CO_CAJA_DEST
AND   T1.AA_CAJA = T2.AA_CAJA_DEST
AND   T1.MM_CAJA = T2.MM_CAJA_DEST
AND   T1.TI_MOVI = T2.TI_MOVI_DEST
AND   T1.NU_COMP_CAJA = T2.NU_COMP_DEST
AND   EXISTS (Select  T2.CO_EMPR	
	       From   TTUSER_CAJA T2
	       Where  T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

/* CALCULANDO TOTAL INGRESOS BANCOS */

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, TO_INGR_BNCO )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM( ISNULL(IN_BNCO,0)),0)
FROM #TWCUAD_CAJA_DIAR_1
GROUP BY CO_EMPR, CO_CAJA


DELETE #TWCUAD_CAJA_DIAR_2

/* GASTOS DEL DIA */

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	

AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.CO_TIPO_OPER != 'A20' and T1.CO_TIPO_OPER != 'A73' and    T1.CO_TIPO_OPER != 'A55')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	    From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	     And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

DELETE #TWCUAD_CAJA_DIAR_3

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR,  CO_CAJA, IM_INGR)--, NU_SOLI)
SELECT T1.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)--, NU_SOLI
FROM   TCMOVI_CAJA T1, TMCAJA T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND   (T1.CO_CAJA != '00000013' and T1.CO_CAJA != '00000014' and T1.CO_CAJA != '00000015'
       and T1.CO_CAJA != '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    (T1.CO_TIPO_OPER != 'A20' AND T1.CO_TIPO_OPER != 'D12')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
AND    T1.CO_EMPR = T2.CO_EMPR
AND    T1.CO_CAJA = T2.CO_CAJA
AND    T2.CO_MONE = @VSCO_MONE
GROUP BY T1.CO_EMPR, T1.CO_CAJA--, T1.NU_SOLI

-- 1

/* -- ORIGINAL 
INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   TCMOVI_CAJA T1, #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.CO_TIPO_OPER != 'A55'
AND    T1.FE_OPER <= @IDFE_OPER
AND    T1.TI_SITU != 'ANU'
AND    T1.NU_SOLI = T2.NU_SOLI
GROUP BY T1.CO_EMPR, T1.CO_CAJA
*/

-- DENIS
/*INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T2.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T2.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND EXISTS ( SELECT T1.CO_EMPR
		FROM TCMOVI_CAJA T1
		WHERE T1.CO_EMPR = T2.CO_EMPR
		AND    T1.TI_MOVI = 'E'
		AND    T1.CO_TIPO_OPER != 'A55'
		AND    T1.FE_OPER <= @IDFE_OPER
		AND    T1.TI_SITU != 'ANU'
		AND    T1.CO_CAJA = T2.CO_CAJA
		AND    T1.NU_SOLI = T2.NU_SOLI)
GROUP BY T2.CO_EMPR, T2.NU_SOLI*/


INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, EG_GAST_DIAS )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM( ISNULL(IM_INGR,0) ) ,0)
FROM #TWCUAD_CAJA_DIAR_2
GROUP BY CO_EMPR, CO_CAJA


/* CALCULANDO NUEVO SALDO CAJA */

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, NU_SALD_CAJA )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM(ISNULL(SA_INIC_TOCA,0)),0) + ISNULL(SUM(ISNULL(TO_INGR_BNCO,0)),0) - 
        ISNULL(SUM (ISNULL(EG_GAST_DIAS,0)),0)
FROM #TWCUAD_CAJA_DIAR_1
GROUP BY CO_EMPR, CO_CAJA

DELETE #TWCUAD_CAJA_DIAR_2

/* CIERRE CAJA */

-- RECIBOS PROVISIONALES

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR, NU_SOLI)
SELECT T1.CO_EMPR, T1.CO_CAJA, 
       ISNULL(SUM(PATINDEX('E', T1.TI_MOVI)*ISNULL(T1.IM_MOVI,0)),0) - 
       ISNULL(SUM(PATINDEX('I', T1.TI_MOVI)*ISNULL(T1.IM_MOVI,0)),0), T1.NU_SOLI
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND    (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
    Or T1.CO_CAJA = '00000016')
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA, T1.NU_SOLI

DELETE #TWCUAD_CAJA_DIAR_3

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)--, NU_SOLI
FROM   TCMOVI_CAJA T1, TMCAJA T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
--AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA != '00000013' and T1.CO_CAJA != '00000014' and T1.CO_CAJA != '00000015'
       and T1.CO_CAJA != '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    (T1.CO_TIPO_OPER != 'A20' AND T1.CO_TIPO_OPER != 'D12')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
AND    T1.CO_EMPR = T2.CO_EMPR
AND    T1.CO_CAJA = T2.CO_CAJA
AND    T2.CO_MONE = @VSCO_MONE
GROUP BY T1.CO_EMPR, T1.CO_CAJA--, T1.NU_SOLI

-- 2
--- ORIGINAL
/*INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   TCMOVI_CAJA T1, #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.CO_TIPO_OPER != 'A55'
AND    T1.FE_OPER <= @IDFE_OPER
AND    T1.TI_SITU != 'ANU'
AND    T1.NU_SOLI = T2.NU_SOLI
GROUP BY T1.CO_EMPR, T1.CO_CAJA*/

-- DENIS
/*INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T2.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T2.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND EXISTS ( SELECT T1.CO_EMPR
		FROM TCMOVI_CAJA T1
		WHERE T1.CO_EMPR = T2.CO_EMPR
		AND    T1.TI_MOVI = 'E'
		AND    T1.CO_TIPO_OPER != 'A55'
		AND    T1.FE_OPER <= @IDFE_OPER
		AND    T1.TI_SITU != 'ANU'
		AND    T1.CO_CAJA = T2.CO_CAJA
		AND    T1.NU_SOLI = T2.NU_SOLI)
GROUP BY T2.CO_EMPR, T2.NU_SOLI*/

SELECT @VNNU_FILA = 0

SELECT @VNNU_FILA = COUNT(CO_CAJA) 
FROM   #TWCUAD_CAJA_DIAR_2

If @VNNU_FILA > 0
   Begin

     UPDATE #TWCUAD_CAJA_DIAR_2 SET SA_INIC_REPR = 
     		                  (SELECT ISNULL(SUM(ISNULL(T1.SA_INIC_REPR,0)),0) 
                                   FROM  #TWCUAD_CAJA_DIAR_1 T1
 				   WHERE T1.CO_EMPR = T2.CO_EMPR AND
					 T1.CO_CAJA = T2.CO_CAJA)
     FROM #TWCUAD_CAJA_DIAR_2 T2
     
     INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, CI_CAJA_PROV)
     SELECT T1.CO_EMPR, T1.CO_CAJA, MAX(ISNULL(T1.SA_INIC_REPR,0)) + ISNULL(SUM(ISNULL(T1.IM_INGR,0) - 
ISNULL(T1.IM_EGRE,0)),0)
     FROM #TWCUAD_CAJA_DIAR_2 T1
     GROUP BY T1.CO_EMPR,T1.CO_CAJA
   End
Else
  Begin
     INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, CI_CAJA_PROV )
     SELECT CO_EMPR, CO_CAJA, ISNULL(SUM(ISNULL(SA_INIC_REPR,0)),0) 
     FROM #TWCUAD_CAJA_DIAR_1
     GROUP BY CO_EMPR, CO_CAJA
  End

DELETE #TWCUAD_CAJA_DIAR_2

--EFECTIVO

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM TCMOVI_CAJA T1

WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND   T1.TI_MOVI = 'I'
AND   T1.FE_OPER = @IDFE_OPER
AND   T1.NU_SOLI IS NULL
AND   T1.CO_TIPO_OPER NOT IN('SIN', '999')
AND   T1.TI_SITU != 'ANU'
AND   EXISTS   (Select  T2.CO_EMPR	
	       From   TTUSER_CAJA T2
	       Where  T1.CO_EMPR = T2.CO_EMPR
	       And    T1.CO_CAJA = T2.CO_CAJA
	       And    T2.CO_USUA = @ISCO_USUA
	       And    T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

-- Provisionales del dða

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_PROV, NU_SOLI)
SELECT T1.CO_EMPR, T1.CO_CAJA,  ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0), T1.NU_SOLI 
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    T1.CO_TIPO_OPER = 'A20'
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA, T1.NU_SOLI

--- Gastos Provisionales

DELETE #TWCUAD_CAJA_DIAR_3

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_GAST)--, NU_SOLI)
SELECT T1.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)--, T1.NU_SOLI
FROM   TCMOVI_CAJA T1, TMCAJA T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
--AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA != '00000013' and T1.CO_CAJA != '00000014' and T1.CO_CAJA != '00000015'
       and T1.CO_CAJA != '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    (T1.CO_TIPO_OPER != 'A20' AND T1.CO_TIPO_OPER != 'D12')
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
AND    T1.CO_EMPR = T2.CO_EMPR
AND    T1.CO_CAJA = T2.CO_CAJA
AND    T2.CO_MONE = @VSCO_MONE
GROUP BY T1.CO_EMPR, T1.CO_CAJA--, T1.NU_SOLI


-- 3 
/* -- ORIGINAL
INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_GAST)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   TCMOVI_CAJA T1, #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.CO_TIPO_OPER != 'A55'
AND    T1.FE_OPER <= @IDFE_OPER
AND    T1.TI_SITU != 'ANU'
AND    T1.NU_SOLI = T2.NU_SOLI
GROUP BY T1.CO_EMPR, T1.CO_CAJA
*/

--DENIS
/*INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_GAST)
SELECT T2.CO_EMPR, @ISCO_CAJA, ISNULL(SUM(ISNULL(T2.IM_PROV,0)),0)
FROM   #TWCUAD_CAJA_DIAR_3 T2
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T2.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND EXISTS ( SELECT T1.CO_EMPR
		FROM TCMOVI_CAJA T1
		WHERE T1.CO_EMPR = T2.CO_EMPR
		AND    T1.TI_MOVI = 'E'
		AND    T1.CO_TIPO_OPER != 'A55'
		AND    T1.FE_OPER <= @IDFE_OPER
		AND    T1.TI_SITU != 'ANU'
		AND    T1.CO_CAJA = T2.CO_CAJA
		AND    T1.NU_SOLI = T2.NU_SOLI)
GROUP BY T2.CO_EMPR, T2.NU_SOLI*/

--DEVOLUCIONES 

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_INGR)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM  TCMOVI_CAJA T1
WHERE ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND   ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND   T1.TI_MOVI = 'I'
AND   T1.FE_OPER = @IDFE_OPER
AND   (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND   T1.TI_SITU != 'ANU'
AND   EXISTS ( Select T2.CO_EMPR	
	       From   TTUSER_CAJA T2
	       Where  T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
AND   NOT EXISTS (SELECT T3.CO_EMPR FROM TDMOVI_RECA T3
WHERE T1.CO_EMPR = T3.CO_EMPR_DEST
AND   T1.CO_CAJA = T3.CO_CAJA_DEST
AND   T1.AA_CAJA = T3.AA_CAJA_DEST
AND   T1.MM_CAJA = T3.MM_CAJA_DEST
AND   T1.TI_MOVI = T3.TI_MOVI_DEST
AND   T1.NU_COMP_CAJA = T3.NU_COMP_DEST)
GROUP BY T1.CO_EMPR, T1.CO_CAJA

-- REEMBOLSOS

INSERT INTO #TWCUAD_CAJA_DIAR_2 (CO_EMPR, CO_CAJA, IM_EGRE)
SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.IM_MOVI,0)),0)
FROM   TCMOVI_CAJA T1
WHERE  ( ( @ISCO_EMPR IS NOT NULL AND T1.CO_EMPR = @ISCO_EMPR ) OR @ISCO_EMPR IS NULL )	
AND    ( ( @ISCO_CAJA IS NOT NULL AND T1.CO_CAJA = @ISCO_CAJA ) OR @ISCO_CAJA IS NULL )	
AND   (T1.CO_CAJA = '00000013' OR T1.CO_CAJA = '00000014' Or T1.CO_CAJA = '00000015'
       Or T1.CO_CAJA = '00000016')
AND    T1.TI_MOVI = 'E'
AND    T1.FE_OPER = @IDFE_OPER
AND    (T1.NU_SOLI IS NOT NULL AND LTRIM(RTRIM(T1.NU_SOLI)) != '')
AND    T1.CO_TIPO_OPER != 'A20'
AND    T1.TI_SITU != 'ANU'
AND    EXISTS (Select  T2.CO_EMPR	
	       From 	TTUSER_CAJA T2
	       Where 	T1.CO_EMPR = T2.CO_EMPR
	       And T1.CO_CAJA = T2.CO_CAJA
	       And T2.CO_USUA = @ISCO_USUA
	       And T2.ST_MANT_INEG = 'S' )
GROUP BY T1.CO_EMPR, T1.CO_CAJA

SELECT @VNNU_FILA = 0

SELECT @VNNU_FILA = COUNT(CO_CAJA) 
FROM   #TWCUAD_CAJA_DIAR_2

If @VNNU_FILA > 0
   Begin

     UPDATE #TWCUAD_CAJA_DIAR_2 SET SA_INIC_EFEC = (SELECT ISNULL(SUM(ISNULL(T1.SA_INIC_EFEC,0)),0) 
                                                    FROM  #TWCUAD_CAJA_DIAR_1 T1
 				                    WHERE T1.CO_EMPR = T2.CO_EMPR AND
					            T1.CO_CAJA = T2.CO_CAJA),
				    IN_BNCO =      (SELECT ISNULL(SUM(ISNULL(T1.IN_BNCO,0)),0) 
  FROM  #TWCUAD_CAJA_DIAR_1 T1
 				                    WHERE T1.CO_EMPR = T2.CO_EMPR AND
					            T1.CO_CAJA = T2.CO_CAJA),
				    EG_GAST_DIAS = ( SELECT ISNULL(SUM(ISNULL(T1.EG_GAST_DIAS ,0)),0) 
						     FROM  #TWCUAD_CAJA_DIAR_1 T1
 				                     WHERE T1.CO_EMPR = T2.CO_EMPR AND
					             T1.CO_CAJA = T2.CO_CAJA)

     FROM #TWCUAD_CAJA_DIAR_2 T2

     INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, CI_CAJA_EFEC )
     SELECT T1.CO_EMPR, T1.CO_CAJA, MAX(ISNULL(T1.SA_INIC_EFEC,0)) + 
            MAX(ISNULL(T1.IN_BNCO,0)) - MAX(ISNULL(T1.EG_GAST_DIAS,0)) + 
          ( ISNULL(SUM( ISNULL(T1.IM_INGR,0) ),0) - ISNULL(SUM( ISNULL(T1.IM_EGRE,0) ),0)) -
          ISNULL(SUM( ISNULL(T1.IM_PROV,0) ),0) + ISNULL(SUM( ISNULL(T1.IM_GAST,0) ),0)
     FROM #TWCUAD_CAJA_DIAR_2 T1

     GROUP BY T1.CO_EMPR, T1.CO_CAJA
   End
ELSE
  Begin
INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, CI_CAJA_EFEC )
     SELECT CO_EMPR, CO_CAJA, ISNULL(SUM(ISNULL(SA_INIC_EFEC,0)),0) + 
            ISNULL(SUM( ISNULL(IN_BNCO,0) ),0) - ISNULL(SUM( ISNULL(EG_GAST_DIAS,0) ),0) 
     FROM #TWCUAD_CAJA_DIAR_1
     GROUP BY CO_EMPR, CO_CAJA
  End

/* CALCULANDO TOTAL CIERRE CAJA */

INSERT INTO #TWCUAD_CAJA_DIAR_1 (CO_EMPR, CO_CAJA, CI_CAJA_TOTA )
SELECT CO_EMPR, CO_CAJA, ISNULL(SUM(ISNULL(CI_CAJA_PROV,0)),0) + ISNULL(SUM(ISNULL(CI_CAJA_EFEC,0)),0)
FROM #TWCUAD_CAJA_DIAR_1
GROUP BY CO_EMPR, CO_CAJA

SELECT T1.CO_EMPR, T1.CO_CAJA, ISNULL(SUM(ISNULL(T1.SA_INIC_EFEC,0)),0) , ISNULL(SUM(ISNULL(T1.SA_INIC_REPR,0)),0),
ISNULL(SUM(ISNULL(T1.SA_INIC_TOCA,0)),0) , ISNULL(SUM(ISNULL(T1.IN_BNCO,0)),0),
ISNULL(SUM(ISNULL(T1.TO_INGR_BNCO,0)),0) , ISNULL(SUM(ISNULL(T1.EG_GAST_DIAS,0)),0) , 
ISNULL(SUM(ISNULL(T1.NU_SALD_CAJA,0)),0) , ISNULL(SUM(ISNULL(T1.CI_CAJA_PROV,0)),0) ,  
ISNULL(SUM(ISNULL(T1.CI_CAJA_EFEC,0)),0) , ISNULL(SUM(ISNULL(T1.CI_CAJA_TOTA,0)),0) ,
MAX(T2.DE_NOMB_CORT), MAX(T3.DE_CAJA)
FROM #TWCUAD_CAJA_DIAR_1 T1, TMEMPR T2, TMCAJA T3
WHERE T1.CO_EMPR = T2.CO_EMPR
AND   T1.CO_EMPR = T3.CO_EMPR
AND   T1.CO_CAJA = T3.CO_CAJA
GROUP BY T1.CO_EMPR, T1.CO_CAJA

RETURN


GO
